package zero.mybatisplus.dynamictable.demo.service;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import zero.mybatisplus.dynamictable.demo.entity.MessageEntity;

@Service
public class MessageConsumerService extends Thread {
    @Autowired
    private RedisTemplate<String, MessageEntity> redisTemplate;

    private volatile boolean flag = true;

    private String queueKey="queue";

    private Long popTimeout = 1000L;

    @Override
    public void run() {
        try {
            MessageEntity message;
            while(flag && !Thread.currentThread().isInterrupted()) {
                message = redisTemplate.opsForList().rightPop(queueKey, popTimeout, TimeUnit.SECONDS);
                System.out.println("接收到了" + message);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

}
